package nfes.the.main;

import java.io.FileNotFoundException;
import java.io.FileReader;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.mapper.CannotResolveClassException;
import com.thoughtworks.xstream.mapper.MapperWrapper;
import java.security.NoSuchAlgorithmException;
import nfes.the.classes.Cabecalho;
import nfes.the.classes.ConsultaLote;
import nfes.the.classes.Erro;
import nfes.the.classes.RetornoLoteRPS;
import nfes.the.util.Constantes;
import nfes.the.util.Util;

/**
 * @author Julianna Paz 
 */
//colocar id nas tag
public class LerXML {

    public static void main(String args[]) throws NoSuchAlgorithmException {
        buscarBeanPorXml();
    }

    /**
     * Usada para tratar o xml retornado do servidor
     */
    public static void buscarBeanPorXml() {
        try {
            FileReader ler = new FileReader(Constantes.caminhoSalvarXMLRetorno + "Lote09052015.xml");
            XStream xStream = new XStream(new DomDriver()) {
                protected MapperWrapper wrapMapper(MapperWrapper next) {
                    return new MapperWrapper(next) {
                        public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                            try {
                                return definedIn != Object.class || realClass(fieldName) != null;
                            } catch (CannotResolveClassException cnrce) {
                                return false;
                            }
                        }
                    };
                }
            };
            xStream.alias("ns1:RetornoEnvioLoteRPS", RetornoLoteRPS.class);
            xStream.aliasField("Erro", Erro.class, "erros");
            xStream.alias("Erro", Erro.class);
            RetornoLoteRPS retorno = (RetornoLoteRPS) xStream.fromXML(ler);
            System.out.println("Data Envio: " + retorno.getCabecalho().getDataEnvioLote());
            System.out.println("Qtde Notas Processadas: " + retorno.getCabecalho().getQtdNotasProcessadas());
            for(Erro wrong: retorno.getErros()){
                System.out.println("Código: " + wrong.getCodigo());
                System.out.println("Descrição: " + wrong.getDescricao());
            }
            
            if(!retorno.getErros().isEmpty()){
                Cabecalho cabecalho = new Cabecalho();
                cabecalho.setCodCidade(retorno.getCabecalho().getCodCidade());
                cabecalho.setCPFCNPJRemetente(retorno.getCabecalho().getCPFCNPJRemetente());
                cabecalho.setVersao(retorno.getCabecalho().getVersao());
                cabecalho.setNumeroLote(retorno.getCabecalho().getNumeroLote());
                ConsultaLote cl = new ConsultaLote(cabecalho);
                gerarXmlBean(cl, "RemessaConsultaLote");
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
     public static void gerarXmlBean(Object objeto, String nomeArquivo) {
        XStream xStream = new XStream(new DomDriver("UTF-8"));
        String encoding = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \n";
        xStream.aliasAttribute("xmlns:ns1", "ns1");
        xStream.aliasAttribute("xmlns:tipos", "tipos");
        xStream.aliasAttribute("xmlns:xsi", "xmnlsXsi");
        xStream.aliasAttribute("xsi:schemaLocation", "xsiSchemaLocation");
        xStream.useAttributeFor(ConsultaLote.class, "ns1");
        xStream.useAttributeFor(ConsultaLote.class, "tipos");
        xStream.useAttributeFor(ConsultaLote.class, "xmnlsXsi");
        xStream.useAttributeFor(ConsultaLote.class, "xsiSchemaLocation");
        xStream.autodetectAnnotations(true);
        Util.gerarArquivo(Constantes.caminhoArquivoLote, nomeArquivo, "xml", encoding + xStream.toXML(objeto));
    }
}
